//
//  NBWelcomeViewController.swift
//  NBSinaWeibo
//
//  Created by ac on 16/5/23.
//  Copyright © 2016年 ac. All rights reserved.
//

import UIKit
import SDWebImage

class NBWelcomeViewController: UIViewController {
    
    private lazy var iconView: UIImageView = UIImageView(image: UIImage(named: "avatar_default_big"))
    private lazy var bgView: UIImageView = UIImageView(image: UIImage(named: "ad_background"))
    private lazy var nameLbl: UILabel = UILabel(title: NBUserAccountViewModel.sharedViewModel.userAccount?.name ?? "", fontSize: 15.0, textColor: UIColor.darkGrayColor())
    
    override func loadView() {
        view = bgView
    }
    
    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        iconAnimation()
    }

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        updateUI()
    }
    
    override func prefersStatusBarHidden() -> Bool {
        return true
    }
    
    func iconAnimation() -> () {
        
        UIView.animateWithDuration(1.2, delay: 0, usingSpringWithDamping: 0.8, initialSpringVelocity: 5, options: [], animations: { 
            self.iconView.snp_updateConstraints(closure: { (make) in
                make.bottom.equalTo(self.bgView.snp_bottom).offset(-400)
                
            })
            self.bgView.layoutIfNeeded()
            }) { (_) in
                UIView.animateWithDuration(0.5, animations: { 
                    self.nameLbl.alpha = 1
                    }, completion: { (_) in
                        NSNotificationCenter.defaultCenter().postNotificationName(changeKeyWindowView, object: "main")
                })
                
        }
        
    }

    func updateUI() -> () {
        view.addSubview(iconView)
        view.addSubview(nameLbl)
        
        iconView.snp_makeConstraints { (make) in
            make.centerX.equalTo(view.snp_centerX)
            make.bottom.equalTo(view.snp_bottom).offset(-160)
            make.width.equalTo(90)
            make.height.equalTo(90)
        }
        nameLbl.snp_makeConstraints { (make) in
            make.centerX.equalTo(iconView.snp_centerX)
            make.top.equalTo(iconView.snp_bottom).offset(16)
        }
//        iconView.contentMode = .ScaleToFill
        iconView.sd_setImageWithURL(NBUserAccountViewModel.sharedViewModel.headUrl)
        iconView.cornerRadius = iconView.bounds.size.width * 0.5
        iconView.layer.masksToBounds = true
        nameLbl.alpha = 0;

        
    }
    
}
